package cn.iocoder.yudao.module.water.controller.admin.month;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.month.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.month.ReportMonthDO;
import cn.iocoder.yudao.module.water.convert.month.ReportMonthConvert;
import cn.iocoder.yudao.module.water.service.month.ReportMonthService;

@Api(tags = "管理后台 - 月报")
@RestController
@RequestMapping("/water/report-month")
@Validated
public class ReportMonthController {

    @Resource
    private ReportMonthService reportMonthService;

    @PostMapping("/create")
    @ApiOperation("创建月报")
    @PreAuthorize("@ss.hasPermission('water:report-month:create')")
    public CommonResult<Long> createReportMonth(@Valid @RequestBody ReportMonthCreateReqVO createReqVO) {
        return success(reportMonthService.createReportMonth(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新月报")
    @PreAuthorize("@ss.hasPermission('water:report-month:update')")
    public CommonResult<Boolean> updateReportMonth(@Valid @RequestBody ReportMonthUpdateReqVO updateReqVO) {
        reportMonthService.updateReportMonth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除月报")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:report-month:delete')")
    public CommonResult<Boolean> deleteReportMonth(@RequestParam("id") Long id) {
        reportMonthService.deleteReportMonth(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得月报")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<ReportMonthRespVO> getReportMonth(@RequestParam("id") Long id) {
        ReportMonthDO reportMonth = reportMonthService.getReportMonth(id);
        return success(ReportMonthConvert.INSTANCE.convert(reportMonth));
    }

    @GetMapping("/list")
    @ApiOperation("获得月报列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<List<ReportMonthRespVO>> getReportMonthList(@RequestParam("ids") Collection<Long> ids) {
        List<ReportMonthDO> list = reportMonthService.getReportMonthList(ids);
        return success(ReportMonthConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得月报分页")
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<PageResult<ReportMonthRespVO>> getReportMonthPage(@Valid ReportMonthPageReqVO pageVO) {
        PageResult<ReportMonthDO> pageResult = reportMonthService.getReportMonthPage(pageVO);
        return success(ReportMonthConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出月报 Excel")
    @PreAuthorize("@ss.hasPermission('water:report-month:export')")
    @OperateLog(type = EXPORT)
    public void exportReportMonthExcel(@Valid ReportMonthExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReportMonthDO> list = reportMonthService.getReportMonthList(exportReqVO);
        // 导出 Excel
        List<ReportMonthExcelVO> datas = ReportMonthConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "月报.xls", "数据", ReportMonthExcelVO.class, datas);
    }

    @GetMapping("/get-one-month-report")
    @ApiOperation("获得一个月月报")
    @ApiImplicitParam(name = "date", value = "时间", required = true, example = "1024", dataTypeClass = Date.class)
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<List<ReportMonthRespVO>> getReportMonth(@RequestParam("date") String date) {
        List list = reportMonthService.getReportOneMonthList(date);
        return success(ReportMonthConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/get-5eday")
    @ApiOperation("获得5天数据")
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<List<ReportMonthRespVO>> get5EDay() {
        List list = reportMonthService.get5EDay();
        return success(ReportMonthConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/get-n-day")
    @ApiOperation("获得某几天数据")
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<List<ReportMonthRespVO>> getNDay(@Valid @RequestBody NDayReqVO vo) {
        List list = reportMonthService.getNDay(vo);
        return success(ReportMonthConvert.INSTANCE.convertList(list));
    }

}
